﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cairo;

namespace BT_DoHoa_Nhom13.GraphicLib
{
    public class PNGCairoGraphic:MyGraphic
    {
        Surface surface;
        Context context;
        String filename;
        int imgWidth, imgHeight;

        static MyGraphic instance;
        public static MyGraphic GetInstance(String path, int width, int height)
        {
            if (instance == null)
            {
                instance = new PNGCairoGraphic(path, width, height);
            }

            return instance;
        }

        private PNGCairoGraphic(String path,int panelWidth, int panelHeight)
        {
            name = ".PNG Cairo Graphic";
            filename = path;

            imgWidth = panelWidth;
            imgHeight = panelHeight;

            surface = new ImageSurface(Format.ARGB32, imgWidth, imgHeight);
            context = new Context(surface);
        }

        
        public override void DrawLine(int x1, int y1, int x2, int y2)
        {
            context.MoveTo(x1, y1);
            context.LineTo(x2, y2);
            context.Stroke();
            surface.WriteToPng(filename);
        }

        public override void DrawRectangle(int x1, int y1, int width, int height, Boolean isFill)
        {
            context.Rectangle(x1,y1,width,height);
            if (isFill)
            {
                context.Color = new Color(0, 0, 0);
            }
            else
            {
                context.Color = new Color(1, 1, 1);
            }

            context.Fill();
            context.Stroke();
            surface.WriteToPng(filename);
        }

        public override void DrawEllipse(int x1, int y1, int width, int height, Boolean isFill)
        {
            context.Save();
            context.Translate(x1+width/2, y1+height/2);
            context.Arc(0, 0, (width > height ? width : height) / 2, 0, 2 * Math.PI);
            if (isFill)
            {
                context.Color = new Color(0, 0, 0);
            }
            else
            {
                context.Color = new Color(1, 1, 1);
            }

            context.Fill();
            context.Stroke();
            context.Restore();
            surface.WriteToPng(filename);
        }

        public override void DrawArc(int x1, int y1, int width, int height, float angleStart, float angleSweep)
        {
            context.Save();
            context.Translate(x1 + width / 2, y1 + height / 2);
            angleStart = (float)(2 * Math.PI - angleStart * Math.PI / 180);
            angleSweep = (float)(2 * Math.PI - angleSweep * Math.PI / 180 - angleStart);
            context.Arc(0, 0, (width > height ? width : height) / 2,angleStart,angleSweep);
            context.Stroke();
            context.Restore();
            surface.WriteToPng(filename);
        }

        public override void DrawString(int x1, int y1, int width, int height, string content)
        {
            context.SelectFontFace("Courier",FontSlant.Normal,FontWeight.Normal);
            context.SetFontSize(10);
            TextExtents extent = context.TextExtents(content);
            context.MoveTo(x1 + width/2 - extent.Width/2,y1+ height/2);
            context.ShowText(content);
            context.Stroke();
            surface.WriteToPng(filename);
        }

        public override void DrawRoundRectangle(int x, int y, int width, int height,int radius, Boolean isFill)
        {

            if ((radius > height / 2) || (radius > width / 2))
                radius = Math.Min(height / 2, width / 2);

            context.MoveTo(x, y + radius);
            context.Arc(x + radius, y + radius, radius, Math.PI, -Math.PI / 2);
            context.LineTo(x + width - radius, y);
            context.Arc(x + width - radius, y + radius, radius, -Math.PI / 2, 0);
            context.LineTo(x + width, y + height - radius);
            context.Arc(x + width - radius, y + height - radius, radius, 0, Math.PI / 2);
            context.LineTo(x + radius, y + height);
            context.Arc(x + radius, y + height - radius, radius, Math.PI / 2, Math.PI);
            

            context.Fill();
            context.ClosePath();
            if (isFill)
            {
                context.Color = new Color(0, 0, 0);
            }
            else
            {
                context.Color = new Color(1, 1, 1);
            }
            context.Stroke();

            surface.WriteToPng(filename);
        }

        public override void SetGraphicInfo(System.Drawing.Graphics graphic, string path)
        {
            filename = path;
        }
        
    }
}
